<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="故障分类" prop="faultId">
        <el-select clearable v-model="queryParams.faultId" placeholder="请选择故障分类" class="!w-240px">
          <el-option v-for="dict in FaultClassificationList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="紧急程度" prop="urgency">
        <el-select v-model="queryParams.urgency" placeholder="请选择紧急程度" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.URGENCY)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.REPAIR_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="下达时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始" end-placeholder="结束" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="申请单号" align="center" prop="repairNo" width="150">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openViewDialog(row.id,)">
            {{ row.repairNo }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="equipmentName" width="120px">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openEquipmentViewDialog(row.equipmentId)">
            {{ row.equipmentName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备编码" align="center" prop="code" width="120" />
      <el-table-column label="设备型号" align="center" prop="equipmentModel" width="120" />
      <el-table-column label="所属部门" align="center" prop="deptName" width="120" />
      <el-table-column label="维修次数" align="center" prop="repairCount" width="120" />
      <el-table-column label="维修时长(小时)" align="center" prop="repairHours" width="120" />
      <el-table-column label="维修成本(元)" align="center" prop="totalCost" width="120" />

      <el-table-column label="操作" align="center" fixed="right" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openHistoryRecords(scope.row)">
            历史维修记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RepairTaskForm ref="formRef" @success="getList" />

  <!-- 维修申请单详细信息 -->
  <RepairTaskInfoView ref="viewRef" />

  <!-- 设备详细信息 -->
  <EquipmentInfoView ref="equipmentView" />

  <!-- <Dialog title="维修历史记录" v-model="dialogVisibleHistory" width="70vw" :fullscreen="true">
    <historyRecords ref="historyRef" />
  </Dialog> -->

  <el-drawer v-model="dialogVisibleHistory" :with-header="false" size="70%">
    <historyRecords ref="historyRef" />
  </el-drawer>


</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { RepairRequestApi, RepairRequest } from '@/api/business/repairrequest'
import RepairRequestForm from '../repairrequest/RepairRequestForm.vue'
import { FaultClassificationApi, FaultClassification } from '@/api/business/faultclassification'
import RepairInfoView from '../repairrequest/RepairInfoView.vue'
import EquipmentInfoView from '../equipmentmanage/EquipmentInfoView.vue'
import { RepairTaskApi, RepairTask } from '@/api/business/repairtask'
import RepairTaskForm from './RepairTaskForm.vue'
import RepairTaskInfoView from './RepairTaskInfoView.vue'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import historyRecords from './historyRecords.vue'

/** 维修申请 列表 */
defineOptions({ name: 'RepairRequest' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisibleHistory = ref(false)
const historyRef = ref()

const loading = ref(true) // 列表的加载中
const list = ref<RepairRequest[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  faultId: undefined,
  equipmentName: undefined,
  urgency: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RepairTaskApi.getRepairRequestTaskStatistics(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}



/** 历史维修记录 */
const openHistoryRecords = async (row) => {
  dialogVisibleHistory.value = true
  await nextTick()
  historyRef.value.initData(row)

}

//报工按钮
const reporting = () => {

}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: RepairRequest[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RepairRequestApi.exportRepairRequest(queryParams)
    download.excel(data, '维修申请.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}


//故障分类列表
const FaultClassificationList = ref([])

//初始化数据
const initData = async () => {
  FaultClassificationList.value = await FaultClassificationApi.getFaultClassificationList();
}

//维修详细信息
const viewRef = ref()

//维修申请详细信息
const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id, true)
}

//设备详细信息
const equipmentView = ref()
//设备详细信息
const openEquipmentViewDialog = (id) => {
  equipmentView.value.openDetailInfo(id)
}


/** 初始化 **/
onMounted(() => {
  getList()
  initData()
})
</script>
